<!DOCTYPE html>
<html>
<head>
    

    

    



    <meta charset="utf-8">
    
    
    
    
    <title>关于跨域 | Anson&#39;s Blog | 种一棵树最好的时间在十年前，其次是现在。</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="JavaScript,NodeJS">
    <meta name="description" content="在使用Vue搭建的一个后端管理系统中，我使用axios请求本地的Node环境下的接口，但是请求失败，然后我错误信息是：                                                                                             331552554043_.pic.jpg             大概意思就是不能访问http">
<meta name="keywords" content="JavaScript,NodeJS">
<meta property="og:type" content="article">
<meta property="og:title" content="关于跨域">
<meta property="og:url" content="http://yoursite.com/2019/03/15/关于跨域/index.html">
<meta property="og:site_name" content="Anson&#39;s Blog">
<meta property="og:description" content="在使用Vue搭建的一个后端管理系统中，我使用axios请求本地的Node环境下的接口，但是请求失败，然后我错误信息是：                                                                                             331552554043_.pic.jpg             大概意思就是不能访问http">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2019/3/15/169808e13bfe3533?w=1159&h=38&f=jpeg&s=12458">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2019/3/15/169808e13be099dc?w=207&h=85&f=jpeg&s=4222">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2019/3/15/169808e13bc07597?w=1240&h=792&f=jpeg&s=63249">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2019/3/15/169808e13bdda705?w=1111&h=815&f=jpeg&s=297478">
<meta property="og:updated_time" content="2019-03-15T12:32:26.666Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="关于跨域">
<meta name="twitter:description" content="在使用Vue搭建的一个后端管理系统中，我使用axios请求本地的Node环境下的接口，但是请求失败，然后我错误信息是：                                                                                             331552554043_.pic.jpg             大概意思就是不能访问http">
<meta name="twitter:image" content="https://user-gold-cdn.xitu.io/2019/3/15/169808e13bfe3533?w=1159&h=38&f=jpeg&s=12458">
    
        <link rel="alternate" type="application/atom+xml" title="Anson&#39;s Blog" href="/atom.xml">
    
    <link rel="shortcut icon" href="/img/favicon.ico">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@latest/css/style.css">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    

</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu"  >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/img/brand.jpg)">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="https://yscoder.github.io/img/face.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">张宁乐</h5>
          <a href="mailto:zhangningle2017@gmail.com" title="zhangningle2017@gmail.com" class="mail">zhangningle2017@gmail.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                归档
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                标签
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/about"  >
                <i class="icon icon-lg icon-user"></i>
                关于
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com/ansonznl" target="_blank" >
                <i class="icon icon-lg icon-github"></i>
                Github
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">关于跨域</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">关于跨域</h1>
        <h5 class="subtitle">
            
                <time datetime="2019-03-15T12:04:55.000Z" itemprop="datePublished" class="page-time">
  2019-03-15
</time>


            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap post-toc-shrink" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#为什么会有跨域"><span class="post-toc-number">1.</span> <span class="post-toc-text">为什么会有跨域</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#什么是跨域"><span class="post-toc-number">1.1.</span> <span class="post-toc-text">什么是跨域</span></a></li></ol></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#怎么解决跨域"><span class="post-toc-number">2.</span> <span class="post-toc-text">怎么解决跨域</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#JSONP"><span class="post-toc-number">2.1.</span> <span class="post-toc-text">JSONP</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#CORS"><span class="post-toc-number">2.2.</span> <span class="post-toc-text">CORS</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#Server-Proxy"><span class="post-toc-number">2.3.</span> <span class="post-toc-text">Server Proxy</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#总结"><span class="post-toc-number">2.4.</span> <span class="post-toc-text">总结</span></a></li></ol></li></ol>
        </nav>
    </aside>


<article id="post-关于跨域"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">关于跨域</h1>
        <div class="post-meta">
            <time class="post-time" title="2019-03-15 20:04:55" datetime="2019-03-15T12:04:55.000Z"  itemprop="datePublished">
2019-03-15
<!--2019-03-15 20:32:26-->
</time>

            


            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <p>在使用Vue搭建的一个后端管理系统中，我使用axios请求本地的Node环境下的接口，但是请求失败，然后我错误信息是：<br><figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="https://user-gold-cdn.xitu.io/2019/3/15/169808e13bfe3533?w=1159&h=38&f=jpeg&s=12458" alt="331552554043_.pic.jpg" title="">
                </div>
                <div class="image-caption">331552554043_.pic.jpg</div>
            </figure><br>大概意思就是不能访问<code>http://localhost:8080</code><br>我的Vue项目端口是<code>http://localhost:8081</code>，Node服务端运行在<code>http://localhost:8080</code>端口上，也就是说因为请求端口和响应端口不一致，所以请求失败。<br>我也在网上查看了一些关于跨域出现的原因及解决的方法，并记录下来。</p>
<h2 id="为什么会有跨域"><a href="#为什么会有跨域" class="headerlink" title="为什么会有跨域"></a>为什么会有跨域</h2><blockquote>
<p>跨域一句话的理解就是：服务端和请求端的地址不一样。 </p>
</blockquote>
<h3 id="什么是跨域"><a href="#什么是跨域" class="headerlink" title="什么是跨域"></a>什么是跨域</h3><p> Ajax 的便利性大家都清楚，可以在不向服务器提交完整的页面的情况下，实现局部更新页面。但是浏览器处于对安全方面的考虑，不允许跨域调用其他页面的对象。<br>其实这个也不能怪浏览器，假设谁都可以随随便便向你发送请求，那样有很大的安全隐患。<br>根据浏览器的同源策略, 只有当协议，域名，端口相同的时候才算是同源, 反之则均视为是一个跨域的请求.<br>也就是说我刚刚的Vue端口是<code>8081</code>，服务端端口是<code>8080</code>，端口不一样，因为同源策略的存在 ，所有我的请求会失败。    </p>
<blockquote>
<p>一个问题，当找到了原因，这个问题就解决了一半了。</p>
</blockquote>
<h2 id="怎么解决跨域"><a href="#怎么解决跨域" class="headerlink" title="怎么解决跨域"></a>怎么解决跨域</h2><p>下面就先介绍三种跨全域的方法：</p>
<h3 id="JSONP"><a href="#JSONP" class="headerlink" title="JSONP"></a>JSONP</h3><p>应该是最常见解决跨域的方法了，<br>他为什么能解决跨域呢，是因为Web 页面上调用 js 文件不受浏览器同源策略的影响，所以通过 Script 便签可以进行跨域的请求：</p>
<ol>
<li>首先前端先设置好回调函数，并将其作为 url 的参数。</li>
<li>服务端接收到请求后，通过该参数获得回调函数名，并将数据放在参数中将其返回</li>
<li>收到结果后因为是 script 标签，所以浏览器会当做是3脚本进行运行，从而达到跨域获取数据的目的。<br>我的前端是<code>index.html</code>，后端是<code>server.js</code><br>后端逻辑：<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">//server.js</span><br><span class="line">const url = require(&apos;url&apos;);</span><br><span class="line">const http = require(&apos;http&apos;);</span><br><span class="line"></span><br><span class="line">http.createServer((req, res)=&gt;&#123;</span><br><span class="line">const data = &#123;</span><br><span class="line">    x: 10//返回的数据</span><br><span class="line">&#125;;</span><br><span class="line">const callback = url.parse(req.url, true).query.callback;</span><br><span class="line">res.writeHead(200);</span><br><span class="line">res.end(`$&#123;callback&#125;($&#123;JSON.stringify(data)&#125;)`);</span><br><span class="line">//执行回调函数，返回data</span><br><span class="line">&#125;).listen(3000, &apos;localhost&apos;);</span><br><span class="line"></span><br><span class="line">console.log(&apos;启动服务，监听 localhost:3000&apos;);</span><br></pre></td></tr></table></figure>
</li>
</ol>
<p>然后使用<code>node server.js</code>运行<br>前端：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">//index.html</span><br><span class="line">&lt;body&gt;</span><br><span class="line">    &lt;script&gt;</span><br><span class="line">	function jsonpCallback(data) &#123;</span><br><span class="line">	    console.log(&apos;获得 X 数据:&apos; + data.x);</span><br><span class="line">	&#125;</span><br><span class="line">    &lt;/script&gt;</span><br><span class="line">    &lt;script src=&quot;http://localhost:3000?callback=jsonpCallback&quot;&gt;&lt;/script&gt;</span><br><span class="line">&lt;/body&gt;</span><br></pre></td></tr></table></figure></p>
<p>之后打开index.html;就可以在控制台看到返回的数据了：<br><figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="https://user-gold-cdn.xitu.io/2019/3/15/169808e13be099dc?w=207&h=85&f=jpeg&s=4222" alt="341552556856_.pic.jpg" title="">
                </div>
                <div class="image-caption">341552556856_.pic.jpg</div>
            </figure></p>
<p>至此，通过 JSONP 跨域获取数据已经成功了，jsonp这种方法跨域，他的兼容性很好，可以在古老的浏览器中国使用，因为这种方法是利用了<code>&lt;script&gt;</code>标签的特殊性，所有只支持GET请求。</p>
<h3 id="CORS"><a href="#CORS" class="headerlink" title="CORS"></a>CORS</h3><p>CORS 是一个 W3C 标准，全称是”跨域资源共享”（Cross-origin resource sharing）它允许浏览器向跨源服务器，发出 XMLHttpRequest 请求，从而克服了 ajax 只能同源使用的限制。</p>
<p>CORS 需要浏览器和服务器同时支持才可以生效，对于开发者来说，CORS 通信与同源的 ajax 通信没有差别，代码完全一样。浏览器一旦发现 ajax 请求跨源，就会自动添加一些附加的头信息，有时还会多出一次附加的请求，但用户不会有感觉。</p>
<p>因此，实现 CORS 通信的关键是服务器。只要服务器实现了 CORS 接口，就可以跨源通信。</p>
<p>前端：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&lt;script src=&quot;https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js&quot;&gt;&lt;/script&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">$.ajax(&#123;</span><br><span class="line">    url:&quot;http://127.0.0.1:3000&quot;,</span><br><span class="line">    success:function(res)&#123;</span><br><span class="line">        var res = JSON.parse(res);</span><br><span class="line">        $(&apos;body&apos;).text(res.data);</span><br><span class="line">        console.log(res.data);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure></p>
<p>这次前端启动需要使用<code>node-server</code>来启动，使用<code>npm install node-server</code>下载，然后当前目录下使用<code>node-server</code>就可以了<br>后端：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">const http = require(&apos;http&apos;);</span><br><span class="line"></span><br><span class="line">http.createServer((req, res)=&gt;&#123;</span><br><span class="line">const data = &#123;</span><br><span class="line">    &apos;data&apos;: &apos;Hello world&apos;//返回的数据</span><br><span class="line">&#125;;</span><br><span class="line">res.writeHead(200, &#123;&apos;Access-Control-Allow-Origin&apos;: &apos;http://127.0.0.1:8080&apos;&#125;);</span><br><span class="line">//设置的头部信息需要和前端请求的地址一致</span><br><span class="line">res.end(JSON.stringify(data));</span><br><span class="line">//返回data</span><br><span class="line">&#125;).listen(3000, &apos;127.0.0.1&apos;);</span><br><span class="line"></span><br><span class="line">console.log(&apos;启动服务，监听 127.0.0.1:3000&apos;);</span><br></pre></td></tr></table></figure></p>
<p>使用命令<code>node server.js</code>启动；<br><figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="https://user-gold-cdn.xitu.io/2019/3/15/169808e13bc07597?w=1240&h=792&f=jpeg&s=63249" alt="211552638161_.pic_hd.jpg" title="">
                </div>
                <div class="image-caption">211552638161_.pic_hd.jpg</div>
            </figure></p>
<p>CORS与JSONP的使用目的相同，但是比JSONP更强大。</p>
<p>JSONP只支持GET请求，CORS支持所有类型的HTTP请求。JSONP的优势在于支持老式浏览器，以及可以向不支持CORS的网站请求数据。</p>
<h3 id="Server-Proxy"><a href="#Server-Proxy" class="headerlink" title="Server Proxy"></a>Server Proxy</h3><p>服务器代理，顾名思义，当你需要有跨域的请求操作时发送请求给后端，让后端帮你代为请求，然后最后将获取的结果发送给你。</p>
<p>假设有这样的一个场景，你的页面需要获取 <a href="http://link.zhihu.com/?target=https%3A//cnodejs.org/api" target="_blank" rel="noopener">CNode：Node.js专业中文社区</a> 论坛上一些数据，如通过 <a href="https://cnodejs.org/api/v1/topics" target="_blank" rel="noopener">https://cnodejs.org/api/v1/topics</a>，当时因为不同域，所以你可以将请求后端，让其对该请求代为转发。</p>
<p>后端代码如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">const url = require(&apos;url&apos;);</span><br><span class="line">const http = require(&apos;http&apos;);</span><br><span class="line">const https = require(&apos;https&apos;);</span><br><span class="line"></span><br><span class="line">http.createServer((req, res)=&gt;&#123;</span><br><span class="line">const path = url.parse(req.url).path.slice(1);</span><br><span class="line">//核对请求路由是否一致</span><br><span class="line">if(path === &apos;topics&apos;)&#123;</span><br><span class="line">    https.get(&apos;https://cnodejs.org/api/v1/topics&apos;, (resp)=&gt;&#123;</span><br><span class="line">        //https代发请求</span><br><span class="line">        let data=&apos;&apos;;</span><br><span class="line">        resp.on(&apos;data&apos;, chunk=&gt;&#123;</span><br><span class="line">            data+= chunk</span><br><span class="line">        &#125;);</span><br><span class="line">        resp.on(&apos;end&apos;, ()=&gt;&#123;</span><br><span class="line">            res.writeHead(</span><br><span class="line">                200,</span><br><span class="line">                &#123;&apos;Content-Type&apos;: &apos;application/json; charset=utf-8&apos;&#125;</span><br><span class="line">            );</span><br><span class="line">            res.end(data);</span><br><span class="line">            //返回数据</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#125;).listen(3000, &apos;127.0.0.1&apos;);</span><br><span class="line"></span><br><span class="line">console.log(&apos;启动服务，监听 127.0.0.1:3000&apos;);</span><br></pre></td></tr></table></figure></p>
<p>前端代码：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&lt;script src=&quot;https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js&quot;&gt;&lt;/script&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">$.ajax(&#123;</span><br><span class="line">    url:&quot;https://cnodejs.org/api/v1/topics&quot;,</span><br><span class="line">    success:function(res)&#123;</span><br><span class="line">        $(&apos;body&apos;).text(JSON.stringify(res));</span><br><span class="line">        console.log(res);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure></p>
<p>这样就成功了<br><figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="https://user-gold-cdn.xitu.io/2019/3/15/169808e13bdda705?w=1111&h=815&f=jpeg&s=297478" alt="221552639459_.pic_hd.jpg" title="">
                </div>
                <div class="image-caption">221552639459_.pic_hd.jpg</div>
            </figure></p>
<h3 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h3><p>常用的跨域方式基本就是这三种：</p>
<ol>
<li>JSONP<br>优点是可以兼容老浏览器，缺点是只能发送GET请求</li>
<li>CORS<br>优点简单方便，支持post请求，缺点是需要后端的配合,不支持老版浏览器。。</li>
<li>Server Proxy<br>优点是前端正常发送ajax请求，缺点是后端会二次请求。</li>
</ol>
<p>其他的跨域方式还有：<code>location.hash</code>、<code>window.name</code>、<code>postMessage</code>等方式，有时间也可以了解一下。</p>
<p>参考资料：</p>
<ul>
<li>跨域资源共享 CORS 详解[阮一峰的博客]：<a href="http://www.ruanyifeng.com/blog/2016/04/cors.html" target="_blank" rel="noopener">http://www.ruanyifeng.com/blog/2016/04/cors.html</a></li>
<li>关于跨域，你想知道的全在这里：<a href="https://zhuanlan.zhihu.com/p/25778815" target="_blank" rel="noopener">https://zhuanlan.zhihu.com/p/25778815</a></li>
<li>不要再问我跨域的问题了[sf]：<a href="https://segmentfault.com/a/1190000015597029" target="_blank" rel="noopener">https://segmentfault.com/a/1190000015597029</a></li>
<li>关于跨域,以及跨域的几种方式[cnblog]：<a href="https://www.cnblogs.com/chenshishuo/p/4919224.html" target="_blank" rel="noopener">https://www.cnblogs.com/chenshishuo/p/4919224.html</a></li>
<li>浏览器的同源策略[MDN]:<a href="https://developer.mozilla.org/zh-CN/docs/Web/Security/Same-origin_policy" target="_blank" rel="noopener">https://developer.mozilla.org/zh-CN/docs/Web/Security/Same-origin_policy</a></li>
</ul>

        </div>

        <blockquote class="post-copyright">
    
    <footer>
        <a href="http://yoursite.com">
            <img src="https://yscoder.github.io/img/face.jpg" alt="张宁乐">
            张宁乐
        </a>
    </footer>
</blockquote>

        


        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/JavaScript/">JavaScript</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/NodeJS/">NodeJS</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://yoursite.com/2019/03/15/关于跨域/&title=《关于跨域》 — Anson's Blog&pic=https://yscoder.github.io/img/face.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://yoursite.com/2019/03/15/关于跨域/&title=《关于跨域》 — Anson's Blog&source=张宁乐,博客" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://yoursite.com/2019/03/15/关于跨域/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《关于跨域》 — Anson's Blog&url=http://yoursite.com/2019/03/15/关于跨域/&via=http://yoursite.com" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://yoursite.com/2019/03/15/关于跨域/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2019/06/04/创建对象的几种方法及优缺点/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">创建对象的几种方法及优缺点</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2019/03/09/Vue中的验证登录状态/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">Vue中的验证登录状态</h4>
      </a>
    </div>
  
</nav>



    











    <!-- Valine Comments -->
    <div class="comments vcomment" id="comments"></div>
    <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
    <script src="//unpkg.com/valine@latest/dist/Valine.min.js"></script>
    <!-- Valine Comments script -->
    <script>
        var GUEST_INFO = ['nick','mail','link'];
        var guest_info = 'nick,mail'.split(',').filter(function(item){
          return GUEST_INFO.indexOf(item) > -1
        });
        new Valine({
            el: '#comments',
            notify: 'false' == 'true',
            verify: 'false' == 'true',
            appId: "gbSwUaucLkuxqOLxm131Mg9U-gzGzoHsz",
            appKey: "b9xU2qIPrm06S0uymbAfrGMD",
            avatar: "mm",
            placeholder: "要不要说点什么？",
            guest_info: guest_info.length == 0 ? GUEST_INFO : guest_info,
            pageSize: "10"
        })
    </script>
    <!-- Valine Comments end -->







</article>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            
                <span><a href="/atom.xml" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            
            <span>博客内容遵循 <a rel="license" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">知识共享 署名 - 非商业性 - 相同方式共享 4.0 国际协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p><span>张宁乐 &copy; 2017 - 2019</span>
            <span>
                
                Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a>
            </span>
			<span>
					Hosted by <a href="https://pages.coding.me" style="font-weight: bold">Coding Pages</a>
			  </span>
        </p>
    </div>
</footer>
<!-- <script>
(function(){
    var bp = document.createElement("script");
    var curProtocol = window.location.protocol.split(":")[0];
    if (curProtocol === "https") {
        bp.src = "https://zz.bdstatic.com/linksubmit/push.js";
    }
    else {
        bp.src = "http://push.zhanzhang.baidu.com/push.js";
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script> -->

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://yoursite.com/2019/03/15/关于跨域/&title=《关于跨域》 — Anson's Blog&pic=https://yscoder.github.io/img/face.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://yoursite.com/2019/03/15/关于跨域/&title=《关于跨域》 — Anson's Blog&source=张宁乐,博客" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://yoursite.com/2019/03/15/关于跨域/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《关于跨域》 — Anson's Blog&url=http://yoursite.com/2019/03/15/关于跨域/&via=http://yoursite.com" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://yoursite.com/2019/03/15/关于跨域/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: false };


</script>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/main.min.js"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/search.min.js" async></script>






<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>





</body>
</html>
